import { _decorator, Component, EditBox, Node, path } from 'cc';
import { Pomelo } from '../libs/Pomelo';
import { ChatView } from './ChatView';
const { ccclass, property } = _decorator;

@ccclass('LoginView')
export class LoginView extends Component {

    @property(Node)
    chatView: Node = null;

    @property(EditBox)
    userName: EditBox = null;

    start() {
        Pomelo.init({
            host: '127.0.0.1',
            port: 34590,
            path: '/'
        }, () => {
            console.log('init success');
        });
    }

    update(deltaTime: number) {
        
    }

    onBtnLoginClicked() {
        const nickname = this.userName.string;
        if (nickname.trim() === '') {
            console.error('请输入昵称');
            return;
        }
        // 发送登录请求
        Pomelo.request('room.room.login', {
            nickname: nickname,

        }, (data) => {
            console.log("data", data);
            if (data.code === 0) {
                // 登录成功
                // 进入游戏场景
                // cc.director.loadScene('GameScene');

                this.chatView.getComponent(ChatView).setNickName(nickname);

                this.node.active = false;
                this.chatView.active = true;
            } else {
                // 登录失败
                // 显示错误信息
                console.error(data.msg);
            }
        });
    }
}


